<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../css/common.css" media="all">
	</head>

	<body>
		<div class="layui-fluid">
			<div class="layui-card">
				<div class="layui-card-header table-operate-btn">
					<button class="layui-btn layui-btn-normal layui-btn-sm" data-type='alladd'>添加</button>
				</div>
				<div class="layui-card-body">
						<div class="layui-row layui-col-space15">
							<div class="layui-btn-sm2 layui-col-md2 layui-col-xs2">
								<div id="test10" class="demo-tree demo-tree-box" style="width: 100%; height: 100%;"></div>
							</div>
							<!-- 搜索 -->
							<div class="layui-btn-sm10 layui-col-md10 layui-col-xs10">
								<form class="layui-form search" id="form" onsubmit="return false" action="##">
									<div class="layui-inline search_inline">
										<label class="layui-form-label">状态：</label>
										<div class="layui-input-inline">
											<select name="status" class="on_off"></select>
										</div>
									</div>
									<button class="layui-btn search_inline" lay-submit="" lay-filter="search" data-type="reload">搜索</button>
									<button class="layui-btn layui-btn-primary search_inline" type="reset">重置</button>
								</form>
								<!-- 搜索 -->
								<table id="test-table-operate" lay-filter="test-table-operate"></table>
								<script type="text/html" id="test-table-operate-barDemo">
									<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑</a>
									<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
								</script>
							</div>
						</div>
				</div>
			</div>
			<script type="text/html" id="zizeng">
				{{d.LAY_TABLE_INDEX+1}}
			</script>
		<script src="../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../js/jquery.min.js"></script>
		<script src="../../../js/common.js"></script>
		<script>
			layui.config({
				base: '../../../layuiadmin/' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'table', 'form','tree'], function() {
				var table = layui.table,
					$ = layui.$,
					form = layui.form,
					laydate = layui.laydate,
					tree = layui.tree,
					admin = layui.admin;

				var _data = {}
				var maplist = JSON.parse(localStorage.getItem('lsmap'))
				initdata();	
				var _data1 = {
					id: 1
				};
				initdata1();
				function initdata1() {
					$.get(server.sys_news_cat_tree_query, _data1, function(res, status) {
						if(status == 'success') {
							if(res.code == 0) {
								var data = res.news_cat.data
								console.log(data)
								data[0].spread = true;
								console.log(data[0].children);
								if (data[0].children) {
									data[0].children[0].spread = true;
								}  							
								tree.render({
									elem: '#test10',
									data: data,
									edit: ['add', 'update', 'del'],
									onlyIconControl: true,
									accordion: true,
									click: function(obj) {
										var data = obj.data
										_data.name = data.title  
										_data.channel_id = data.id == 1 ? '' : data.id
										initdata(); 
									},
									operate: function(obj) {
										var type = obj.type; //得到操作类型：add、edit、del
										var data = obj.data; //得到当前节点的数据
										var elem = obj.elem; //得到当前节点元素
										var id = data.id; //得到节点索引
										console.log(id);
										if(type === 'add') { //增加节点
											layer.confirm('确定要提交数据？', function() {
												var _data = {
													fid: id
												}
												submitdata(server.addsubchannel, _data);
											}, function() {
												initdata();
												layer.msg('取消操作');
											});
										} else if(type === 'update') { //修改节点
											var _data = {
												id: id,
												name: elem.find('.layui-tree-txt').html()
											}
											submitdata(server.editsubchannel, _data);
										} else if(type === 'del') { //删除节点
											var _data = {
												id: id
											}
											submitdata(server.deletesubchannel, _data);
										};
									}
								});
							} else {
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}
				function initdata() {
						table.render({
							elem: '#test-table-operate',
							url: server.sys_news_query,
							height: 'full-150',
							cellMinWidth: cellMinWidth,
							cols: [
								[{
								width: 50,
								field: 'zizeng',
								title: '#',
								fixed: 'left',
								templet: '#zizeng'
							},{
									width: 180,
									align: 'center',
									toolbar: '#test-table-operate-barDemo',
									title: '操作',
									fixed: 'left'
								}, {
									field: 'status',
									width: 100,
									title: '状态',
									templet: function(d) {
										var html = ''
										if(d.status == 1) {
											html = '<input type="checkbox" name="status" value=' + d.id +
												' lay-skin="switch" lay-text="启用|停用" lay-filter="status" checked>'
										} else {
											html = '<input type="checkbox" name="status" value=' + d.id +
												' lay-skin="switch" lay-text="启用|停用" lay-filter="status">'
										}
										return html;
									}
								},
//								{
//									field: 'cat_name',
//									title: '分类名称',
//									width: 150,
//								},
								{
									field: 'channel_name',
									title: '频道名称',
									width: 150,
								}, {
									field: 'title',
									width: 150,
									title: '标题',
								},{
									field: 'intro',
									width: 100,
									title: '文章简介',
								}, {
									field: 'content',
									width: 150,
									title: '文章内容',
								}, {
									field: 'keywords',
									width: 150,
									title: '关键词',
								},{
									field: 'author',
									width: 150,
									title: '作者',
								},{
									field: 'description',
									width: 150,
									title: '描述',
								},{
									field: 'resource',
									width: 150,
									title: '来源',
								},{
									field: 'resource_url',
									width: 150,
									title: '来源地址',
								},{
									field: 'hits',
									width: 150,
									title: '浏览量',
								},{
									field: 'likes',
									width: 150,
									title: '点赞量',
								},{
									field: 'url',
									width: 150,
									title: '文章链接',
								},{
									field: 'url_short',
									width: 150,
									title: '文章短连接',
								},{
									field: 'add_by',
									title: '创建人',
								}, {
									field: 'addtime',
									title: '创建时间',
								}, {
									field: 'edit_by',
									title: '更新人',
								}, {
									field: 'edittime',
									title: '更新时间',
								}]
							],
							page: true,
							where: _data,
							limit: 20,
							request: {
								pageName: 'page',
								limitName: 'size',
							},
							parseData: function(res) { //将原始数据解析成 table 组件所规定的数据
								return {
									"code": res.code, //解析接口状态
									"msg": res.msg, //解析提示文本
									"count": res.news.pager.total, //解析数据长度
									"data": res.news.data //解析数据列表
								};
							},
							done: function() {
								//lay-tree的颜色 
								var nodes = document.getElementsByClassName("layui-tree-txt");
								for(var i = 0; i < nodes.length; i++) {
									if(nodes[i].innerHTML === _data.name)
										nodes[i].style.color = "red";
									else
										nodes[i].style.color = "#555";
								}
							}
						});
					}
				form.on('switch(status)', function(obj) {
					var id = this.value;
					layer.confirm('确定要修改状态？', function(index) {
						if(obj.elem.checked) {
							submitdata1(id, 1)
						} else {
							submitdata1(id, 0)
						}
					}, function() {
						initdata();
						layer.msg('取消操作');
					});

				});
				function submitdata(_url,_data) {
					$.post(_url, _data, function(res, status) {
						if (status == 'success') {
							if (res.code == 0) {
								layer.alert('操作成功', function() {
									layer.closeAll();
									initdata();
									layui.table.reload('test-table-operate');
								});
							} else {
								layer.msg(res.msg);
								initdata();
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}
				function submitdata1(id, status) {
					$.get(server.sys_news_onoff, {
						id: id,
						status: status
					}, function(res, status) {
						if(status == 'success') {
							if(res.code == 0) {
								layer.alert('操作成功', function() {
									layer.closeAll();
								});
							} else {
								layer.msg(res.msg);
								layer.closeAll('loading');
							}
							initdata();
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}
				function submitDelete(id) {
					$.post(server.sys_news_delete, {
						id: id
					}, function(res, status) {
						if(status == 'success') {
							if(res.code == 0) {
								layer.alert('操作成功', function() {
									layer.closeAll(); 
									table.reload('test-table-operate');
								});
							} else {
								layer.msg(res.msg);
								layer.closeAll('loading');
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}
				//监听搜索
				form.verify({
					id: function(value, item) { //value：表单的值、item：表单的DOM对象
						if(!value) {
							return '会员编号不能为空';
						}
					}
				});

				//监听搜索
				form.on('submit(search)', function(data) {
					_data = data.field;
					initdata();
					return false;
				});

				$("button[type=reset]").on("click", function() {
					_data = {}
					initdata();
					document.getElementById("form").reset();
				});

				//监听表格复选框选择
				table.on('checkbox(test-table-operate)', function(obj) {
					console.log(obj)
				});

				//监听工具条
				table.on('tool(test-table-operate)', function(obj) {
					var data = obj.data;
					if(obj.event === 'del') {
						layer.confirm('真的删除行么', function(index) {
							submitDelete(data.id);
//							obj.del();
							layer.close(index);
						});
					}else if(obj.event === 'seeheadimg') {
							var img_data = {
								"data": [{
									"src": data.thumbnail 
								}]
							}
							layer.photos({
								photos: img_data,
								anim: 5
							});
					}else if(obj.event === 'edit') {
						layer.open({
							type: 2,
							title: '编辑新闻',
							shadeClose: false,
							shade: 0.5,
							maxmin: true, //开启最大化最小化按钮
							area: ['600px', '650px'],
							content: 'model/pic_edit_add.html?id=' + data.id,
							success: function(layero, index) {
								layer.full(index);
							},
							end: function() {}
						});
					}
				});
					var active = { 
						alladd: function() {
							layer.open({
								type: 2,
								title: '添加新闻',
								shadeClose: false,
								shade: 0.5,
								maxmin: true, //开启最大化最小化按钮
								area: ['1200px', '800px'],
								content: 'model/pic_edit_add.html',
								success: function(layero, index) {
									layer.full(index);
								},
								end: function() {
									initdata()
								}
							});
						}, 
					}
				$('.table-operate-btn .layui-btn').on('click', function() {
					var type = $(this).data('type');
					active[type] ? active[type].call(this) : '';
				});

				$('#del').on('click', function() {
					layer.confirm('真的删除数据吗？', function(index) {
						// obj.del();
						layer.close(index);
					});
				});
			});
		</script>
	</body>

</html>